package act.droidcar;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.droidcar.AdapterSppinerCombustiveis;
import com.droidcar.AdapterSppinerCores;
import com.droidcar.AdapterSppinerKms;
import com.droidcar.AdapterSppinerMarcas;
import com.droidcar.AdapterSppinerModelos;
import com.droidcar.ClientPesquisar;
import com.droidcar.CombustivelBD;
import com.droidcar.CorBD;
import com.droidcar.DataBaseCreate;
import com.droidcar.KmBD;
import com.droidcar.ListaVeiculosBD;
import com.droidcar.MarcaBD;
import com.droidcar.ModeloBD;
import com.droidcar.VeiculosBD;


/**
 * Activity para pesquisa de veiculos atravez de WebService
 */
public class TelaPesquisar extends Activity{

	private DataBaseCreate baseDados;
	private SQLiteDatabase objDados;
	
	private KmBD		  oKm;			
	private CombustivelBD oCombustivel;
	private CorBD 		  oCor;	
	private MarcaBD 	  oMarca;	
	private ModeloBD  	  oModelo;
	
	private AdapterSppinerMarcas 		adapterMarcas;
	private AdapterSppinerModelos 		adapterModelos;
	private AdapterSppinerCombustiveis  adapterCombustiveis;
	private AdapterSppinerCores 		adapterCores;
	private AdapterSppinerKms 			adapterKms;
	

	Spinner spnMarcas;
	Spinner spnModelos;
	Spinner spnKm;
	Spinner spnCor;
	Spinner spnCombustivel;
	EditText edtAnoInicial;
	EditText edtAnoFinal;
	EditText edtValorInicial;
	EditText edtValorFinal;	
	Button btnPesquisar;
	Button btnVoltar;

	
	/**
	 * Inicilizacao de componentes e eventos.
	 */	
	public void CarregarComponentes(){
		setContentView(R.layout.pesquisar);	

				
		edtAnoInicial	  = (EditText) findViewById(R.campo.AnoInicial);
		edtAnoFinal	 	  = (EditText) findViewById(R.campo.AnoFinal);
		edtValorInicial   = (EditText) findViewById(R.campo.ValorInicial);
		edtValorFinal	  = (EditText) findViewById(R.campo.ValorFinal);
		
		spnMarcas		  = (Spinner) findViewById(R.campo.marcas);
		spnModelos		  = (Spinner) findViewById(R.campo.modelos);
		
		spnCombustivel	  = (Spinner) findViewById(R.campo.combustivel);
		spnCor		  	  = (Spinner) findViewById(R.campo.cores);
		spnKm		 	  = (Spinner) findViewById(R.campo.kmmaxima);	
		
		btnPesquisar = (Button) findViewById(R.botao.pesquisar);
		btnVoltar    = (Button) findViewById(R.botao.voltar);
		
		btnPesquisar.setOnClickListener( new View.OnClickListener() {
			
			public void onClick(View arg0) {
				
				
				int Marca		 = 0;
				int Modelo   	 = 0;
				int Cor			 = -1;
				int Combustivel  = -1;
				float KmMaxima	 = -1;			
				int AnoInicial	 = -1;
				int AnoFinal   	 = -1;
				float ValorInicial = -1;
				float ValorFinal   = -1;				
				
				//Faz a validação dos campos e a pesquisa para então redirecionar para a lista de resultados
				if( !edtAnoInicial.getText().equals(null) || !edtAnoFinal.getText().equals(null)){
					
					if( edtAnoInicial.getText().equals(null) )
						AnoInicial = -1;
					else
						AnoInicial = Integer.parseInt(edtAnoInicial.getText().toString());
						
					if( edtAnoFinal.getText().equals(null) )
						AnoFinal = -1;
					else
						AnoFinal   = Integer.parseInt(edtAnoFinal.getText().toString());					
				}
				
				if( !edtValorInicial.getText().equals(null) || !edtValorFinal.getText().equals(null)){
					
					if( edtValorInicial.getText().equals(null) )
						ValorInicial = -1;
					else
						ValorInicial = Integer.parseInt(edtValorInicial.getText().toString());
						
					if( edtValorFinal.getText().equals(null) )
						ValorFinal = -1;
					else
						ValorFinal = Integer.parseInt(edtValorFinal.getText().toString());
				}
					
				Marca		 = adapterMarcas.getItem(spnMarcas.getSelectedItemPosition()).getMCA_CODIGO();
				Modelo   	 = adapterModelos.getItem(spnModelos.getSelectedItemPosition()).getMDO_CODIGO();
				Cor			 = adapterCores.getItem(spnCor.getSelectedItemPosition()).getCOR_CODIGO();
				Combustivel  = adapterCombustiveis.getItem(spnCombustivel.getSelectedItemPosition()).getCMB_CODIGO();
				KmMaxima	 = adapterKms.getItem(spnKm.getSelectedItemPosition()).getKM_ATE();
					
				ClientPesquisar cp = new ClientPesquisar();
				ArrayList<VeiculosBD> veiculos = cp.PesquisarVeiculos(Marca, Modelo, Combustivel, Cor, ValorInicial, ValorFinal, AnoInicial, AnoFinal, KmMaxima);
				if( veiculos == null ){
						
		    		Intent listaVeiculos = new Intent(TelaPesquisar.this,ListarVeiculos.class);
		    		ListaVeiculosBD obj = new ListaVeiculosBD();
		    		obj.addLista(veiculos);
		    			
		    		listaVeiculos.putExtra("ListaVeiculos", obj); 
		    		listaVeiculos.putExtra("Tipo", 2); 
		    		TelaPesquisar.this.startActivity(listaVeiculos);   							
					
				}else{
					Toast.makeText(TelaPesquisar.this, "Nenhum registro localizado!", Toast.LENGTH_LONG).show();
				}
				
				
			}
			
		});
		
		btnVoltar.setOnClickListener( new View.OnClickListener() {
			
			public void onClick(View arg0) {
				
				finish();
								
			}
			
		});		
		
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pesquisar);		
		
		CarregarComponentes();
		
		baseDados = new DataBaseCreate(this);
		objDados  = baseDados.retornaBaseDados();
		
		oKm				= new KmBD(objDados);
		oCombustivel	= new CombustivelBD(objDados);
		oCor			= new CorBD(objDados);
		oMarca			= new MarcaBD(objDados);
		oModelo			= new ModeloBD(objDados);
		
        createListView(oMarca,oModelo,oCombustivel,oCor,oKm);
		spnMarcas.setOnItemSelectedListener( new OnItemSelectedListener() {
			
		    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
		    	adapterModelos   	= new AdapterSppinerModelos(getApplicationContext(), oModelo.SELECT(" MCA_CODIGO = "+adapterMarcas.getItem(position).getMCA_CODIGO()));
		    	spnModelos.setAdapter(adapterModelos);		    	
		    }

		    public void onNothingSelected(AdapterView<?> parentView) {
		        // your code here
		    }

		});

		
	}
	
	/**
	 * Inicilizacao dos componentes Spinner.
	 */	
    private void createListView(MarcaBD marca, ModeloBD modelo, CombustivelBD comb, CorBD cor, KmBD km) {    

       adapterMarcas 		= new AdapterSppinerMarcas(this, marca.SELECT(""));   
   	   adapterModelos   	= new AdapterSppinerModelos(this, modelo.SELECT(" MCA_CODIGO = "+adapterMarcas.getItem(0).getMCA_CODIGO()));
   	   adapterCombustiveis  = new AdapterSppinerCombustiveis(this, comb.SELECT(""));
   	   adapterCores			= new AdapterSppinerCores(this, cor.SELECT(""));
   	   adapterKms			= new AdapterSppinerKms(this, km.SELECT(""));        
       spnMarcas.setAdapter(adapterMarcas);
       spnModelos.setAdapter(adapterModelos);
       spnCombustivel.setAdapter(adapterCombustiveis);
       spnCor.setAdapter(adapterCores);
       spnKm.setAdapter(adapterKms);	    
   }



}
